package com.realdtech.wechat.task.Service;


import com.jfinal.plugin.activerecord.Db;
import com.realdtech.wechat.task.model.DitchCredit;
import com.realdtech.wechat.task.model.DitchEnterprise;
import com.realdtech.wechat.task.model.DitchMortgage;
import com.realdtech.wechat.task.model.DitchVehicle;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/19.
 */
public class DitchService {

    private static final DitchCredit DC=new DitchCredit().dao();
    private static final DitchVehicle DV=new DitchVehicle().dao();
    private static final DitchMortgage DM=new DitchMortgage().dao();
    private static final DitchEnterprise DE=new DitchEnterprise().dao();

    /**
     *
     * @param qd    渠道名称
     * @param ed    贷款额度
     * @param ll    贷款利率
     * @param sd    批款速度
     * @param qx    贷款期限
     * @param fs    还款方式
     * @param lx    产品类型
     */
    //保存表 信用贷款
    public void add_financial(String image, String qd, String ed, String ll, String sd, String qx, String fs,String lx,String rt ){

        DitchCredit dc=new DitchCredit();
        dc.setImage(image);   //保存上传的头像
        dc.setImageName(qd);
        dc.setLimit(ed);
        dc.setInterest(ll);
        dc.setSpeed(sd);
        dc.setDate(new Date());
        dc.setDeadline(qx);
        dc.setRepayment(fs);
        dc.setProduct(lx);
        dc.setRequirement(rt);

        dc.save();
    }
    //车辆贷款
    public void add_V(String image, String qd, String ed, String ll, String sd, String qx, String fs,String lx,String rt ){

        DitchVehicle dc=new DitchVehicle();
        dc.setImage(image);   //保存上传的头像
        dc.setImageName(qd);
        dc.setLimit(ed);
        dc.setInterest(ll);
        dc.setSpeed(sd);
        dc.setDate(new Date());
        dc.setDeadline(qx);
        dc.setRepayment(fs);
        dc.setProduct(lx);
        dc.setRequirement(rt);

        dc.save();
    }
    //房屋贷款
    public void add_M(String image, String qd, String ed, String ll, String sd, String qx, String fs,String lx,String rt ){

        DitchMortgage dc=new DitchMortgage();
        dc.setImage(image);   //保存上传的头像
        dc.setImageName(qd);
        dc.setLimit(ed);
        dc.setInterest(ll);
        dc.setSpeed(sd);
        dc.setDate(new Date());
        dc.setDeadline(qx);
        dc.setRepayment(fs);
        dc.setProduct(lx);
        dc.setRequirement(rt);

        dc.save();
    }
    //企业贷款
    public void add_E(String image, String qd, String ed, String ll, String sd, String qx, String fs,String lx,String rt ){

        DitchEnterprise dc=new DitchEnterprise();
        dc.setImage(image);   //保存上传的头像
        dc.setImageName(qd);
        dc.setLimit(ed);
        dc.setInterest(ll);
        dc.setSpeed(sd);
        dc.setDate(new Date());
        dc.setDeadline(qx);
        dc.setRepayment(fs);
        dc.setProduct(lx);
        dc.setRequirement(rt);

        dc.save();
    }

    /**
     * 查ditch表 信用贷款
     * @return
     */

    public List<DitchCredit> find(){
        return DC.find("SELECT * FROM  ditch_credit");
    }

    //随机查询4条数据，用于显示首页
    public List<DitchCredit> findD_4(){
        return DC.find("SELECT * FROM ditch_credit order by rand() limit  4");
    }

    //根据id查询
    public DitchCredit findD(String id){
        return DC.findFirst("SELECT * FROM  ditch_credit WHERE id="+id+"");
    }

    //查询单条数据
    public DitchCredit findDs(String id){
        return DC.findFirst("SELECT * FROM  ditch_credit WHERE id="+id+"");
    }

    //更新表 头像 名称 额度 利率 天数 还款时间 还款方式 产品类型 申请条件
    public void updateC(String image,String image_name,String limit,String interest,String speed,String deadline,String repayment,String product,String requirement,String id){
        Db.update("update ditch_credit dc set image='"+image+"' , image_name='"+image_name+"',dc.limit='"+limit+"',interest='"+interest+"',speed='"+speed+"',deadline='"+deadline+"',repayment='"+repayment+"',product='"+product+"',requirement='"+requirement+"' where id="+id+"");
    }

    //根据id删除
    public void delete_D(String id){

        Db.update("delete FROM ditch_credit WHERE id = "+id+" ");
    }

    /**
     * 查询vehicle表 车辆贷款 ditch_vehicle
     * @return
     */

    public List<DitchVehicle> findV(){
        return DV.find("SELECT * FROM  ditch_vehicle");
    }

    //随机查询4条数据
    public List<DitchVehicle> findV_4(){
        return DV.find("SELECT * FROM ditch_vehicle order by rand() limit 4");
    }

    //根据id查询
    public DitchVehicle findV(String id){
        return DV.findFirst("SELECT * FROM  ditch_vehicle WHERE id="+id+"");
    }

    //查询单条数据
    public DitchVehicle findVs(String id){
        return DV.findFirst("SELECT * FROM  ditch_vehicle WHERE id="+id+"");
    }

    //更新表 头像 名称 额度 利率 天数 还款时间 还款方式 产品类型 申请条件
    public void updateV(String image,String image_name,String limit,String interest,String speed,String deadline,String repayment,String product,String requirement,String id){
        Db.update("update ditch_vehicle dv set image='"+image+"' , image_name='"+image_name+"',dv.limit='"+limit+"',interest='"+interest+"',speed='"+speed+"',deadline='"+deadline+"',repayment='"+repayment+"',product='"+product+"',requirement='"+requirement+"' where id="+id+"");
    }

    //根据id删除
    public void delete_V(String id){
        Db.update("delete FROM ditch_vehicle WHERE id = "+id+" ");
    }

    /**
     * 查询mortgage表 房屋贷款 ditch_mortgage
     * @return
     */

    public List<DitchMortgage>findM(){
        return DM.find("SELECT * FROM  ditch_mortgage");
    }

    //随机查询本表4条数据
    public List<DitchMortgage>findM_4(){
        return DM.find("SELECT * FROM ditch_mortgage order by rand() limit 4");
    }

    //根据id查询
    public DitchMortgage findM(String id){
        return DM.findFirst("SELECT * FROM  ditch_mortgage WHERE id="+id+"");
    }

    //查询单条数据
    public DitchMortgage findMs(String id){
        return DM.findFirst("SELECT * FROM  ditch_mortgage WHERE id="+id+"");
    }

    //更新表 头像 名称 额度 利率 天数 还款时间 还款方式 产品类型 申请条件
    public void updateM(String image,String image_name,String limit,String interest,String speed,String deadline,String repayment,String product,String requirement,String id){
        Db.update("update ditch_mortgage dm set image='"+image+"' , image_name='"+image_name+"',dm.limit='"+limit+"',interest='"+interest+"',speed='"+speed+"',deadline='"+deadline+"',repayment='"+repayment+"',product='"+product+"',requirement='"+requirement+"' where id="+id+"");
    }

    //根据id删除
    public void delete_M(String id){
        Db.update("delete FROM ditch_mortgage WHERE id = "+id+" ");
    }


    /**
     * 查询Enterprise表 企业贷款 ditch_enterprise
     * @return
     */

    public List<DitchEnterprise>findE(){
        return DE.find("SELECT * FROM ditch_enterprise");
    }

    //随机查询本表4条数据
    public List<DitchEnterprise>findE_4(){
        return DE.find("SELECT * FROM ditch_enterprise order by rand() limit 4");
    }

    //根据id倒序查询所有数据
    public List<DitchEnterprise>findE_desc(){
        return DE.find("SELECT * FROM  ditch_enterprise order by id desc");
    }

    //根据id查询本表
    public DitchEnterprise findE(String id) {
        return DE.findFirst("SELECT * FROM  ditch_enterprise WHERE id=" + id + "");
    }

    //查询单条数据
    public DitchEnterprise findEs(String id){
        return DE.findFirst("SELECT * FROM  ditch_enterprise WHERE id="+id+"");
    }

    //更新表 头像 名称 额度 利率 天数 还款时间 还款方式 产品类型 申请条件
    public void updateE(String image,String image_name,String limit,String interest,String speed,String deadline,String repayment,String product,String requirement,String id){
        Db.update("update ditch_enterprise de set image='"+image+"' , image_name='"+image_name+"',de.limit='"+limit+"',interest='"+interest+"',speed='"+speed+"',deadline='"+deadline+"',repayment='"+repayment+"',product='"+product+"',requirement='"+requirement+"' where id="+id+"");
    }

    //根据id删除
    public void delete_E(String id){
        Db.update("delete FROM ditch_enterprise WHERE id = "+id+" ");
    }

}
